package csc.fresher.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import csc.fresher.domain.UserInfo;
import csc.fresher.domain.json.UserTemp;

@Repository
public class UserInfoDAOImpl implements UserInfoDAO {

	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public UserInfo getUserInfo(String loginId) {
		List<UserInfo> list = null;
		Query query = sessionFactory
				.getCurrentSession()
				.createQuery(
						"Select a from UserInfo a where a.loginId = :m");
		query.setParameter("m", loginId);
		list = query.list();
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}
	
	@Override
	public UserTemp getUserTest(String loginId) {
		UserInfo user = (UserInfo) sessionFactory.getCurrentSession().get(
				UserInfo.class, loginId);
		
		UserTemp userTemp = new UserTemp(user.getId(),user.getLoginId(), user.getStatus()
				.getStatusid(), user.getRole().getRoleid(), user.getPassword(),
				user.getCardnumberid(), user.getFirstname(),
				user.getLastname(), user.getMidname(), user.getPhone1(),
				user.getPhone2(), user.getAddress1(), user.getAddress2(),
				user.getEmail1(), user.getEmail2(), user.getImage());
		return userTemp;
	}

	@Override
	public List<UserInfo> userInfos() {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				UserInfo.class);
		return criteria.list();
	}

	@Override
	public boolean save(UserInfo UserInfo) {
		sessionFactory.getCurrentSession().save(UserInfo);
		return true;
	}
	
	@Override
	public boolean update(UserInfo UserInfo) {
		sessionFactory.getCurrentSession().update(UserInfo);
		return true;
	}

	@Override
	public List<UserInfo> userNomal() {
		Query query = sessionFactory.getCurrentSession().createQuery(
				"Select a from UserInfo a where a.role.rolename = :l");
		query.setParameter("l", "ROLE_USER");
		return query.list();
	}

	@Override
	public List<UserInfo> usernew() {
		Query query = sessionFactory
				.getCurrentSession()
				.createQuery(
						"Select a from UserInfo a where a.role.rolename != :l and a.status.statusname = :m");
		query.setParameter("l", "ROLE_ADMIN");
		query.setParameter("m", "new");
		return query.list();
	}

	@Override
	public List<UserInfo> useractive() {
		Query query = sessionFactory
				.getCurrentSession()
				.createQuery(
						"Select a from UserInfo a where a.role.rolename != :l and a.status.statusname = :m");
		query.setParameter("l", "ROLE_ADMIN");
		query.setParameter("m", "active");
		return query.list();
	}

	@Override
	public List<UserInfo> userdisable() {
		Query query = sessionFactory
				.getCurrentSession()
				.createQuery(
						"Select a from UserInfo a where a.role.rolename != :l and a.status.statusname = :m");
		query.setParameter("l", "ROLE_ADMIN");
		query.setParameter("m", "disable");
		return query.list();
	}

	@Override
	public boolean checkLoginId(String loginId) {
		Query query = sessionFactory.getCurrentSession().createQuery(
				"select a from UserInfo a where a.loginId = :m");
		query.setParameter("m", loginId);
		List<UserInfo> list = query.list();

		if (list.size() == 0) {
			return false;
		}

		if (list.get(0).getLoginId() != null) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public UserInfo getUserAuthentication(String loginId) {
		List<UserInfo> list = null;
		Query query = sessionFactory
				.getCurrentSession()
				.createQuery(
						"Select a from UserInfo a where a.loginId = :m and a.status.statusname = :n");
		query.setParameter("m", loginId);
		query.setParameter("n", "active");
		list = query.list();
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

}
